package com.invengo.device;

import android.os.Message;
import android.util.Log;

import com.rfid.api.ADReaderInterface;
import com.rfid.api.GFunction;
import com.rfid.api.ISO14443AInterface;
import com.rfid.api.ISO14443ATag;
import com.rfid.api.ISO14443BInterface;
import com.rfid.api.ISO14443BTag;
import com.rfid.api.ISO14443bInterfaceST;
import com.rfid.api.ISO14443bTagST;
import com.rfid.api.ISO15693Interface;
import com.rfid.api.ISO15693Tag;
import com.rfid.def.ApiErrDefinition;
import com.rfid.def.RfidDef;

import java.util.Vector;

import me.goldze.mvvmhabit.utils.KLog;

public class ThreadHostControl implements ThreadInterface {
    private ScanCallback callback;
    private ADReaderInterface m_reader;

    private long mAntCfg = 0x000000;

    private ISO15693Tag ISO15693TagData = new ISO15693Tag();

    private boolean b_inventoryThreadRun = false;

    public ThreadHostControl(ADReaderInterface m_reader, ScanCallback scanCllback) {
        this.m_reader = m_reader;
        this.callback = scanCllback;
    }

    private boolean bMathAFI = false;
    private byte mAFIVal = 0x00;

    @Override
    public void run() {
        int failedCnt = 0;//
        Object hInvenParamSpecList = null;
        byte newAI = RfidDef.AI_TYPE_NEW;
        byte[] useAnt = null;
        if (mAntCfg != 0) {
            Vector<Byte> vAntList = new Vector<>();
            for (int i = 0; i < 32; i++) {
                if ((mAntCfg & (1 << i)) != 0) {
                    vAntList.add((byte) (i + 1));
                }
            }

            useAnt = new byte[vAntList.size()];
            for (int i = 0; i < useAnt.length; i++) {
                useAnt[i] = vAntList.get(i);
            }
        }
        hInvenParamSpecList = ADReaderInterface.RDR_CreateInvenParamSpecList();

        ISO15693Interface.ISO15693_CreateInvenParam(
                hInvenParamSpecList, (byte) 0, bMathAFI, mAFIVal,
                (byte) 0);

        ISO14443BInterface.ISO14443B_CreateInvenParam(hInvenParamSpecList, (byte) 0, (byte) 0, (byte) 0);
        ISO14443bInterfaceST.ISO14443B_CreateInvenParam(hInvenParamSpecList, (byte) 0, (byte) 0, (byte) 0);

        b_inventoryThreadRun = true;
        while (b_inventoryThreadRun) {
            int iret = m_reader.RDR_TagInventory(newAI, useAnt, 0, hInvenParamSpecList);
            int i = m_reader.RDR_GetReaderLastReturnError();
            if (iret == ApiErrDefinition.NO_ERROR || iret == -ApiErrDefinition.ERR_STOPTRRIGOCUR) {
                Vector<Object> tagList = new Vector<>();
                newAI = RfidDef.AI_TYPE_NEW;
                Object tagReport = m_reader.RDR_GetTagDataReport(RfidDef.RFID_SEEK_FIRST);
                while (tagReport != null) {

                    Log.e("xxx", "进入15693");
                    ISO15693Tag ISO15693TagData = new ISO15693Tag();
                    iret = ISO15693Interface.ISO15693_ParseTagDataReport(tagReport, ISO15693TagData);
                    if (iret == ApiErrDefinition.NO_ERROR) {
                        byte[] targetArray=(byte[]) ISO15693TagData.uid.clone();
                        String uid = GFunction.encodeHexStr(targetArray);
                        //block
                        ISO15693Interface mTag = new ISO15693Interface();
                        iret = mTag.ISO15693_Connect(m_reader, ISO15693TagData.tag_id,
                                (byte) 1, ISO15693TagData.uid);
                        if (iret == 0) {
                            int blkAddr = 0;
                            int numOfBlksToRead = 3;
                            long[] numOfBlksRead = new long[1];
                            long[] bytesBlkDatRead = new long[1];
                            byte[] bufBlocks = new byte[4 * numOfBlksToRead];
                            iret = mTag.ISO15693_ReadMultiBlocks(false, blkAddr,
                                    numOfBlksToRead, numOfBlksRead, bufBlocks, bytesBlkDatRead);
                            for (byte bufBlock : bufBlocks) {
                                System.out.println("bufBlock = " + bufBlock);
                            }
                            String strData = GFunction.encodeHexStr(bufBlocks);
                            callback.result(uid,strData);
                            ISO15693TagData.uid = bufBlocks;
                        }

                        // ISO15693 TAG
                        tagReport = m_reader.RDR_GetTagDataReport(RfidDef.RFID_SEEK_NEXT);
                        continue;
                    }
                }

            }
        }
        b_inventoryThreadRun = false;
        m_reader.RDR_ResetCommuImmeTimeout();
    }


    @Override
    public void setThreadRun(boolean run) {
        this.b_inventoryThreadRun = run;
    }

    @Override
    public boolean isThreadRun() {
        return b_inventoryThreadRun;
    }
}
